package com.mytest.leetcode;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by shixi  on 2019/6/24
 */
@Slf4j
public class Solution36 {

    public static void main(String[] args) {
        Solution36 solution = new Solution36();
        char [ ][ ] board= {{'5','3','.','.','7','.','.','.','.'}
            ,{'6','.','.','1','9','5','.','.','.'}
            ,{'.','9','8','.','.','.','.','6','.'}
            ,{'8','.','.','.','6','.','.','.','3'}
            ,{'4','.','.','8','.','3','.','.','1'}
            ,{'7','.','.','.','2','.','.','.','6'}
            ,{'.','6','.','.','.','.','2','8','.'}
            ,{'.','.','.','4','1','9','.','.','5'}
            ,{'.','.','.','.','8','.','.','7','9'}};
        log.info("{}", solution.isValidSudoku(board));
    }
    public boolean isValidSudoku(char[][] board) {
        int[][] rows = new int[9][9];
        int[][] columns = new int[9][9];
        int[][][] subboxes = new int[3][3][9];

        for(int i=0;i<9;i++) {
            for(int j=0;j<9;j++){
                char cur = board[i][j];

                if (cur!='.') {
                    int index = cur - '0' - 1;
                    // 行不能用重复
                    rows[i][index]++;
                    // 列不能重复
                    columns[j][index]++;
                    subboxes[i / 3][j / 3][index]++;
                    if (rows[i][index] > 1 || columns[j][index] > 1 || subboxes[i / 3][j / 3][index] > 1) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
}

